<template>
  <div class="app-container" v-loading="loading">
    <el-form
      ref="queryRef"
      :inline="true"
      :model="queryParams"
      class="form-box"
    >
      <div style="display: flex; flex-wrap: wrap">
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.receiptCode"
              clearable
              placeholder="退货订单"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-select
              clearable
              placeholder="质检状态"
              v-model="queryParams.status"
            >
              <template v-for="(item, index) in receipt_qc_status" :key="index">
                <el-option :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.waybillCode"
              clearable
              placeholder="快递单号"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.skuCode"
              clearable
              placeholder="SKU编码"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.skuName"
              clearable
              placeholder="商品名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.expressName"
              clearable
              placeholder="退货快递"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.customerName"
              clearable
              placeholder="客户名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.customerPhone"
              clearable
              placeholder="手机号"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-date-picker
              style="width: 100%"
              v-model="timeData"
              start-placeholder="客户退货开始"
              end-placeholder="结束时间"
              range-separator="-"
              type="datetimerange"
              value-format="YYYY-MM-DD HH:mm:ss"
              unlink-panels
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button icon="Search" type="primary" @click="getList"
              >查询</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </div>
    </el-form>

    <vxe-table
      style="margin-top: 10px"
      :data="tableData"
      :height="tableHeight"
      align="center"
      border
      :row-config="{ isCurrent: true, isHover: true }"
    >
      <vxe-column title="序号" type="seq" width="60"></vxe-column>
      <vxe-column field="receiptCode" width="150" title="退货订单">
      </vxe-column>
      <vxe-column field="waybillCode" width="100" title="快递单号"></vxe-column>
      <vxe-column field="status" width="150" title="质检状态">
        <template v-slot="{ row }">
          {{ returnDictLabel(receipt_qc_status, row.status) }}
        </template>
      </vxe-column>
      <vxe-column field="skuCode" width="200" title="SKU编码"></vxe-column>
      <vxe-column field="skuName" width="200" title="商品名称"></vxe-column>
      <vxe-column field="coverUrl" width="200" title="商品图片">
        <template #default="{ row }">
          <ImagePreview
            v-if="row.coverUrl"
            :width="150"
            :height="50"
            :src="row.coverUrl"
          ></ImagePreview>
        </template>
      </vxe-column>
      <vxe-column field="totalNum" width="100" title="退货数量"></vxe-column>
      <vxe-column field="expressName" width="120" title="退货快递"></vxe-column>
      <vxe-column
        field="customerName"
        width="120"
        title="客户名称"
      ></vxe-column>
      <vxe-column field="customerPhone" width="150" title="手机号"></vxe-column>
      <vxe-column
        field="customerAddress"
        width="120"
        title="客户地址"
      ></vxe-column>
      <vxe-column
        field="applyReturnTime"
        width="120"
        title="客户退货时间"
      ></vxe-column>
    </vxe-table>
    <pagination
      v-show="total > 0"
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { nextTick, onMounted, ref } from "vue";
import { customerReturnList } from "@/api/scApi";
import ImagePreview from "@/components/ImagePreview/index.vue";

const { proxy } = getCurrentInstance();
const { receipt_qc_status } = proxy.useDict("receipt_qc_status");

let loading = ref(false);
let queryParams = ref({
  pageNum: 1,
  pageSize: 10,
});
let timeData = ref([]);
const tableData = ref([]);
const total = ref(0);
let tableHeight = ref(0);
onMounted(() => {
  nextTick(() => {
    tableHeight.value = window.innerHeight - 360;
  });
  window.onresize = () => {
    tableHeight.value = document.body.scrollHeight - 360;
  };
  getList();
});

function getList() {
  loading.value = true;
  queryParams.value.startTime = timeData.value[0];
  queryParams.value.endTime = timeData.value[1];
  let params = {
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize,
    param: queryParams.value,
  };
  customerReturnList(params)
    .then((res) => {
      loading.value = false;
      if (res.code === 200) {
        tableData.value = res.data;
        total.value = res.total;
      }
    })
    .finally(() => {
      loading.value = false;
    });
}

const resetQuery = () => {
  timeData.value = [];
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
  };
  getList();
};
</script>

<style scoped lang="scss"></style>
